package com;

import javax.swing.*;
import com.trusfort.redis.JedisClient;
import com.trusfort.redis.RedisConnectionPool;
import com.util.FileUtils;
import com.util.Log;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import redis.clients.jedis.exceptions.JedisDataException;

import java.awt.*;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.awt.event.ActionEvent;

public class DevIdAnalysis {
	private static JTextField txtHost;
	private static JTextField txtPort;
	private static JComboBox redisKeyPrefixComboBox;
	private static long total = 0;
	
	public static String[] redisKeys = { 
			"XDID::aimei", 
			"XDID::abmac", 
			"XDID::amac", 
			"XDID::aaid", 
			"XDID::abid", 
			"XDID::absn",
			"XDID::afpid",
			"XDID::afdid", 
			"XDID::asn", 
			"XDID::acid"
			};
	private static JTextArea txtLog;
	private static JTextField txtLimit;
	private static JTextField txtRedisKey;
	private static JTextField txtPassword;
	

	public static void logInfo(String msg) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String cur = sdf.format(new Date());

		System.out.println(msg);
		txtLog.append(msg+"\n");
	}


 

	/**
	 * 统计每个特征key个数
	 */
	public void statFieldCount() {

		try {

			String ip = txtHost.getText();
			int port = Integer.valueOf(txtPort.getText());
			String password = txtPassword.getText();
			JedisClient client = new JedisClient(ip, port,password);

			String  redisKeyPrefix =redisKeyPrefixComboBox.getSelectedItem().toString();
			
			logInfo("插入无效Redis特征数据开始>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
			
			String key = redisKeyPrefix+""+txtRedisKey.getText();
			
			logInfo("Redis key:"+key);
			
			java.util.List<Smember> smemberList = new ArrayList<>();
			
			for(int i=0;i<Integer.valueOf(txtLimit.getText());i++){
			  
			  Smember smember = new Smember(key,UUID.randomUUID().toString().replace("-",""));
			  smemberList.add(smember);
			}
			
			client.sadd(DBIndex.XDID_DB_INDEX,smemberList, 60*60*24*180);  
			
			logInfo("插入无效Redis特征数据结束<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

		} catch (JedisDataException e1) {
			e1.printStackTrace();
		  logInfo("Redis连接异常>>"+e1);
		} catch (Exception e1) {
			e1.printStackTrace();
			logInfo("Redis异常>>"+e1);
		}
	}

	 
	public static void main(String[] args) {
		JFrame jf = new JFrame("Redis数据分析");
		jf.setSize(839, 481);
		jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		jf.setLocationRelativeTo(null);

		// 创建内容面板，指定使用 流式布局
		JPanel panel = new JPanel();
		panel.setLayout(null);
		JButton btn05 = new JButton("分析");
		btn05.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				new Thread(new Runnable() {//启动数据获取，更新显示进程 
 
                public void run() { 
                	DevIdAnalysis main = new DevIdAnalysis();
                	// 获取每种特征key个数
    				main.statFieldCount();
                } 
            }).start(); // 
			}
		});
		btn05.setBounds(704, 33, 69, 23);
		panel.add(btn05);

		jf.setContentPane(panel);

		txtHost = new JTextField();
		txtHost.setText("192.168.1.170");
		txtHost.setBounds(101, 33, 163, 21);
		panel.add(txtHost);
		txtHost.setColumns(10);

		JLabel lblIp = new JLabel("IP:");
		lblIp.setBounds(68, 36, 54, 15);
		panel.add(lblIp);

		JLabel label = new JLabel("端口：");
		label.setBounds(68, 69, 54, 15);
		panel.add(label);

		txtPort = new JTextField();
		txtPort.setText("6386");
		txtPort.setColumns(10);
		txtPort.setBounds(101, 66, 163, 21);
		panel.add(txtPort);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(86, 112, 700, 286);
		panel.add(scrollPane);
		
		txtLog = new JTextArea();
		txtLog.setLineWrap(true);
		scrollPane.setViewportView(txtLog);
		txtLog.setColumns(10);
		
		JLabel label_2 = new JLabel("条数：");
		label_2.setBounds(274, 69, 46, 15);
		panel.add(label_2);
		
		txtLimit = new JTextField();
		txtLimit.setText("10");
		txtLimit.setColumns(10);
		txtLimit.setBounds(330, 66, 119, 21);
		panel.add(txtLimit);
		
		JLabel label_1 = new JLabel("特征：");
		label_1.setBounds(274, 36, 46, 15);
		panel.add(label_1);
		
		redisKeyPrefixComboBox = new JComboBox();
		redisKeyPrefixComboBox.setBounds(330, 33, 119, 21);
		
		for(String key:redisKeys){
		  redisKeyPrefixComboBox.addItem(key);
		}
		
		
		panel.add(redisKeyPrefixComboBox);
		
		JLabel label_3 = new JLabel("特征值：");
		label_3.setBounds(465, 37, 54, 15);
		panel.add(label_3);
		
		txtRedisKey = new JTextField();
		txtRedisKey.setColumns(10);
		txtRedisKey.setBounds(518, 34, 154, 21);
		panel.add(txtRedisKey);
		
		JLabel label_3_1 = new JLabel("密码：");
		label_3_1.setBounds(459, 69, 54, 15);
		panel.add(label_3_1);
		
		txtPassword = new JTextField();
		txtPassword.setColumns(10);
		txtPassword.setBounds(518, 66, 154, 21);
		panel.add(txtPassword);
		jf.setVisible(true); // PS: 最后再设置为可显示(绘制), 所有添加的组件才会显示
	}
}